/**
 * 该文件用于暴露methods属性中一些公用方法提取，使用时以vue混入方式植入组件中
 * author: luanzg
 * date: 20200902
 */
import checkPermission from './permission.js'
import { matTlist } from '@/api/material/mattype.js'

export default {
  data() {
    return {
      sysmatTypeCfg: [
        {
          mat_type_id: 0,
          mat_type_name: '根类型1',
          children: []
        }
      ],
      spanValue: 5,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < (Date.now() - 3600 * 1000 * 24 * 3)
        },
        shortcuts: [
          {
            text: '昨天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          },
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          },
          {
            text: '明天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() + 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }]
      }
    }
  },
  async created() {
    const res = await this.getsysMatTypeList()
    this.sysmatTypeCfg = res.data.data
  },
  updated() {
    // 弹窗组件根组件自定义class添加
    this.$nextTick(() => {
      if (this.$refs.dragDialog !== undefined) {
        setTimeout(() => {
          this.addClass(this.$refs.dragDialog.$el, 'el-dialog__parent')
        }, 500)
      }
    })
  },
  methods: {
    /* 素材类型树获取 */
    getsysMatTypeList() {
      return new Promise((resolve, reject) => {
        matTlist().then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },

    /* 左侧侧边栏抽屉伸缩方法 */
    spanValueChange(val) {
      this.spanValue = val
    },
    /**
     * 指令权限检查方法
     */
    checkPermission,

    /**
     * 该函数用于查找menus级联菜单中指定值的label属性并返回
     * @param {Object} val
     * @param {Object} menus
     */
    findValLabel: function (val, menus, value = 'value', label = 'label') {
      let target = ''
      if (!Array.isArray(menus)) return false
      for (let i = 0; i < menus.length; i++) {
        if (menus[i][value] === parseInt(val)) {
          target = menus[i][label]
          return target
        }

        if (Array.isArray(menus[i].children) && target === '') {
          target = this.findValLabel(val, menus[i].children, value, label)
        }
      }
      return target
    },
    /**
     * 该函数用于返回树形节点包含自已已其子集节点所有value,数组
     */
    findNodeChildValArr: function (Node, val = 'value', nodeArr = [], children = 'children') {
      const arr = JSON.parse(JSON.stringify(nodeArr))

      if (!Array.isArray(Node)) {
        if (Node[val] !== undefined) {
          arr.push(Node[val])
        }

        if (Array.isArray(Node[children])) {
          arr.push(...this.findNodeChildValArr(Node[children], val, arr, children))
        }
      } else {
        for (let i = 0; i < Node.length; i++) {
          if (Node[i][val] !== undefined) {
            arr.push(Node[i][val])
          }

          if (Array.isArray(Node[i][children])) {
            arr.push(...this.findNodeChildValArr(Node[i][children], val, arr, children))
          }
        }
      }

      return Array.from(new Set(arr))
    },
    // 时间帧数进行时间格式转换
    formatMethod(value, fps4k = false, format = 'HH:ii:ss-ms') {
      const fpsNum = parseInt(value)
      const fMaxsm = fps4k ? 20 : 40
      const ssArr = (fpsNum * fMaxsm / 1000).toFixed(2).split('.')
      let thetime = '00' // 秒
      let middle = '00' // 分
      let hour = '00' // 时
      let fps = '00' // 帧
      let result = ''
      thetime = parseInt(ssArr[0])
      if (thetime > 59) {
        middle = parseInt(thetime / 60)
        thetime = parseInt(thetime % 60)

        if (middle > 59) {
          hour = parseInt(middle / 60)
          middle = parseInt(middle % 60)
        }
      }
      // 帧数转换
      if (parseInt(ssArr[1]) > 0) {
        fps = parseInt(ssArr[1]) * 10 / fMaxsm
      }
      result += String(hour).padStart(2, '0') + ':'
      result += String(middle).padStart(2, '0') + ':'
      result += String(thetime).padStart(2, '0')
      if (format === 'HH:ii:ss-ms') {
        result += '-' + String(fps).padStart(2, '0')
      }
      return result
    },

    /* 对指定el节点追加class */
    addClass(element, value) {
      let newClassName
      if (!element.className) {
        element.className = value
      } else {
        newClassName = element.className
        if (!newClassName.split(' ').includes(value)) {
          newClassName += ' '
          newClassName += value
        }
        element.className = newClassName
      }
    },
    removeClass(element, value) {
      let newClassName, newClassArr
      if (!element.className) {
        element.className = ''
      } else {
        newClassName = element.className
        newClassArr = newClassName.split(' ')
        if (newClassArr.indexOf(value) > -1) {
          const index = newClassArr.indexOf(value)
          newClassArr.splice(index, 1)
        }
        element.className = newClassArr.join(' ')
      }
    },
    // 获取当天日期
    getCurrDate(dayStr) {
      var dd = new Date(dayStr || new Date())
      var y = dd.getFullYear()
      var m = dd.getMonth() + 1 // 获取当前月份的日期
      var d = dd.getDate()
      return y + '-' + String(m).padStart('2', '0') + '-' + String(d).padStart('2', '0')
    },
    // 获取当天前面一周日期
    getCurrBeforeWeek(dayStr) {
      var dd = new Date(dayStr || new Date())
      dd.setDate(dd.getDate() - 7)
      var y = dd.getFullYear()
      var m = dd.getMonth() + 1 // 获取当前月份的日期
      var d = dd.getDate()
      return y + '-' + String(m).padStart('2', '0') + '-' + String(d).padStart('2', '0')
    }
  }
}
